package com.aztech.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;

import com.huawei.hms.framework.common.StringUtils;

import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;

public class WebActivity extends AppCompatActivity {

    private static GeckoRuntime sRuntime;
    private GeckoView geckoView;
    private GeckoSession geckoSession;
    private MyNavigationDelegate navigationDelegate = new MyNavigationDelegate();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        String targetURL = "";
        if (getIntent().getExtras() != null) {
            targetURL = getIntent().getExtras().getString("Website");
        }

        geckoView = findViewById(R.id.geckoview);
        geckoSession = new GeckoSession();

        // Workaround for Bug 1758212
        geckoSession.setContentDelegate(new GeckoSession.ContentDelegate() {});

        if (sRuntime == null) {
            // GeckoRuntime can only be initialized once per process
            sRuntime = GeckoRuntime.create(this);
        }

        geckoSession.open(sRuntime);
        geckoView.setSession(geckoSession);
        geckoSession.setNavigationDelegate(navigationDelegate);
        if (targetURL.length() > 0)
            geckoSession.loadUri(targetURL);
        else
            geckoSession.loadUri("http://azp");
    }

    @Override
    public void onBackPressed() {
        if (navigationDelegate.canGoBack) {
            geckoSession.goBack();
        } else {
            super.onBackPressed();
        }
    }


    private class MyNavigationDelegate implements GeckoSession.NavigationDelegate {
        public boolean canGoBack = false;

        @Override
        public void onCanGoBack(GeckoSession session, boolean canGoBack) {
            this.canGoBack = canGoBack;
        }
    }
}


